#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include <stdlib.h>
int main()
{
  pid_t id = fork();
  if(id==0)
  {
    //子进程
    int cnt = 5;
    while(cnt--)
    {
      printf("我是子进程,我的pid = %d, ppid = %d , %d\n",getpid(), getppid(), cnt);
      sleep(2);
    }
    exit(1);
  }
  else if(id>0)
  {
    int status = 0;
    pid_t ret = waitpid(id,&status, 0);  //阻塞式等待
    if(ret>0)
    {
      if(WIFEXITED(status))
      {
        //子进程正常退出
        printf("子进程执行完毕，子进程的退出码位：%d \n",WEXITSTATUS(status));
      }
      else{
        printf("子进程异常退出：%d \n",WIFEXITED(status));
      }
    }
  }
  return 0;
}
